<?php
use Base\AdminController;
use Illuminate\Support\Str;
use JetBrains\PhpStorm\NoReturn;

class YacController extends AdminController
{
    protected string $table = '';

    protected string $model = "";

    #[NoReturn] public function init(): void
    {
        $this->routeWhiteLst = ['yac@index','yac@lst','yac@edit','yac@del','yac@add','yac@clear'];
        parent::init();
    }

    #[NoReturn] public function indexAction(): void
    {
        $result['version'] = YAC_VERSION;
        $result['values_memory_size'] = ini_get("yac.values_memory_size");
        $result['keys_memory_size'] = ini_get("yac.keys_memory_size");
        $result['serializer'] = ini_get("yac.serializer");
        $result['config_version'] = '1.1.2';
        $result['config_directory'] = ini_get('yaconf.directory');
        $result['config_check_delay'] = ini_get('yaconf.check_delay');
        $this->apiSuccess($result,"success",20000);
    }

    #[NoReturn] public function lstAction(): void
    {
        $yac = new Yac(Request::input("app_name")."_");
        $data = $yac->dump(100000);
        foreach ($data as $key=>$vo) {
            if (!str_contains($vo['key'], Request::input("app_name") . "_")){
                unset($data[$key]);
            }
            if ($vo['ttl']==1) {
                unset($data[$key]);
            }
            if (time()>$vo['ttl']&&$vo['ttl']!=0) {
                unset($data[$key]);
            }
        }
        $data = array_values($data);
        if (Request::input("title")) {
            $data = array_filter($data, function ($item) {
                return str_contains($item['key'], Request::input("title"));
            });
        }
        $this->apiSuccess(['items'=>array_values($data)],"success",20000);
    }

    #[NoReturn] public function delAction(): void
    {
        $yac = new Yac();
        if (!Request::input("key")) $this->apiError("key不能为空");
        $res = $yac->delete(Request::input("key"));
        if ($res) {
            $this->apiSuccess([], "success", 20000);
        }else {
            $this->apiError("操作失败");
        }
    }

    /**
     * 添加
     * @return void
     */
    #[NoReturn] public function addAction(): void
    {
        if (Request::instance()->isPost()) {
            $this->update();
        } else {
            $this->operate();
        }
    }

    /**
     * 编辑
     * @return void
     */
    #[NoReturn] public function editAction(): void
    {
        if (Request::instance()->isPost()||Request::instance()->isPut()) {
            $this->update();
        } else {
            $this->operate();
        }
    }

    /**
     * 显示
     * @return void
     */
    #[NoReturn] protected function operate(): void
    {
        try {
            $yac = new Yac();
            $info = $yac->get(Request::input("ids"));
            if (!$info) $this->apiError("数据不存在");
            $result = [
                'key'=>Request::input("ids"),
                'value'=>$info
            ];
            $this->apiSuccess($result,"获取成功",20000);
        }catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    /**
     * 修改
     * @return void
     */
    protected function update(): void
    {
        try {
            $params  = Request::all();
            $yac = new Yac($params['app_name']."_");
            $yac->set(Request::input("key"), Request::input("value"), Request::input("ttl")?:0);
            $this->apiSuccess( [],"新增成功",20000);
        } catch (Exception $e) {
            $this->apiError($e->getMessage());
        }
    }

    #[NoReturn] public function clearAction(): void
    {
        $yac = new Yac();
        $yac->flush();
        $this->apiSuccess([], "清空成功", 20000);
    }

}
